package com.deke.order.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.deke.entity.EduCourse;
import com.deke.entity.EduTeacher;
import com.deke.entity.Order;
import com.deke.entity.TbMember;
import com.deke.order.dao.OrderDao;
import com.deke.order.service.OrderService;
import com.deke.order.vo.OrderVo;
import com.deke.order.vo.PayOrderVo;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Username 刘亦辰（59372312@qq.com）
 * @date ：Created in 2022/12/9 12:13
 * @description：
 * @modified By：
 * @version:
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;


    /**
     * @create by: Username 刘亦辰 
     * @description: 订单系统  模糊查询 
     * @create time: 2022/12/9 12:29
     * @param null
     * @return :  
     **/
    @Transactional
    @Override
    public Result<Map<String,Object>> queryAllByLimit(Integer currentPage, Integer pageSize, OrderVo orderVo) {
        IPage<Order> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (orderVo.getCourseTitle()!=null && orderVo.getCourseTitle()!=""){
            wrapper.like("course_title",orderVo.getCourseTitle());
        }
        if (orderVo.getMobile()!=null && orderVo.getMobile()!=""){
            wrapper.like("mobile",orderVo.getMobile());
        }
        if (orderVo.getOrderNo()!=null && orderVo.getOrderNo()!=""){
            wrapper.like("order_no",orderVo.getOrderNo());
        }
        if (orderVo.getTeacherName()!=null && orderVo.getTeacherName()!=""){
            wrapper.like("teacher_name",orderVo.getTeacherName());
        }
        if (orderVo.getStatus()!=null && orderVo.getStatus()!=""){
            wrapper.eq("status",orderVo.getStatus());
        }
        if (orderVo.getPayType()!=null && orderVo.getPayType()!=""){
            wrapper.eq("pay_type",orderVo.getPayType());
        }
        if (orderVo.getStartTime()!=null && orderVo.getStartTime()!=""){
            wrapper.ge("gmt_create",orderVo.getStartTime());
        }
        if (orderVo.getEndTime()!=null && orderVo.getEndTime()!=""){
            wrapper.le("gmt_create",orderVo.getEndTime());
        }
        IPage<Order> iPage = orderDao.selectPage(page, wrapper);
        Double money = orderDao.paySuccess();
        if (ObjectUtil.isNotNull(iPage)){
            Map map = new HashMap();
            map.put("money",money);
            map.put("ipage",iPage);
            return new Result(2000,"查询成功",map);
        }
        return new Result<>(0000,"失败");
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 根据订单号查询订单 
     * @create time: 2022/12/12 17:10
     * @param null
     * @return :  
     **/
    @Override
    public Result queryOrderAndPayLog(String id) {
        PayOrderVo payOrderVo = orderDao.queryOrderAndPayLog(id);
        if (ObjectUtil.isNotNull(payOrderVo)){
            return  new Result<>(2000,"查询成功",payOrderVo);
        }
        return new Result<>(0000,"查询失败");
    }


    /**
     * @create by: Username 刘亦辰 
     * @description: 查询老师信息 
     * @create time: 2022/12/15 12:04
     * @param null
     * @return :  
     **/
    @Override
    public Result queryAllTeacherBYTeacherName(String teacherName) {
        EduTeacher eduTeacher = orderDao.queryAllTeacherBYTeacherName(teacherName);
        if (ObjectUtil.isNotNull(eduTeacher)){
            return  new Result(2000,"查询成功",eduTeacher);
        }
        return new Result(0000,"查询失败",null);
    }


    /**
     * @create by: Username 刘亦辰 
     * @description: 查询课程的信息 
     * @create time: 2022/12/15 16:49
     * @param null
     * @return :  
     **/
    @Override
    public Result<List<EduCourse>> queryAllCourse(String courseId) {
        List<EduCourse> courseList = orderDao.queryAllCourse(courseId);
        if (ObjectUtil.isNotNull(courseList)&&courseList.size()>0){
            return new Result<>(2000,"查询成功",courseList);
        }
        return new Result(0000,"查询失败",null);
    }


    /**
     * @create by: Username 刘亦辰 
     * @description:  用户信息回显
     * @create time: 2022/12/15 17:53
     * @param null
     * @return :  
     **/
    @Override
    public Result queryAllMemberById(String memberId) {
        TbMember member = orderDao.queryAllMemberById(memberId);
        if (ObjectUtil.isNotNull(member)){
            return new Result(2000,"查询成功",member);
        }
        return new Result(0000,"查询失败",null);
    }


    @Override
    public Result getOrderStatus(String courseId, String memberId) {
        Order order = orderDao.getOrderStatus(courseId, memberId);
        if(ObjectUtil.isNotNull(order)){
            return new Result(2000,"查询订单成功",order);
        }
        return new Result(0000,"查询订单失败",null);
    }

    /**
     * @create by: 陈昂（731712841@qq.com）
     * @description:  删除订单
     * @create time: 2023/2/12 19:38
     * @param null
     * @return
     */
    @Override
    public Result deleteOrder(String id) {
        int i = orderDao.deleteById(id);
        if (i>0){
            return  new Result(2000,"删除成功",null);
        }
        return new Result(2000,"删除失败",null);
    }
}